//
//  HSPWebViewController.m
//  Icar
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPWebViewController.h"
#import "BaseHeader.h"
#import "AppDelegate.h"
@interface HSPWebViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIView *titleView;
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)AppDelegate *appDelegate;
@property (nonatomic,strong)MBProgressHUD *HUD;
@property (strong, nonatomic) FeHandwriting *handwritingLoader;
@property (strong, nonatomic) FeThreeDotGlow *threeDot;
@end

@implementation HSPWebViewController
- (void)viewWillAppear:(BOOL)animated
{
[_webView reload];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 64)];
    [_titleView setBackgroundColor:BLUECOLOR];
    UIImage *image1 = [UIImage imageNamed:@"5.png"];
    UIImage *img1 = [self scaleToSize:image1 size:CGSizeMake(30 * ADJUSTWIDTH, 30 * ADJUSTHEIGHT)];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(10 * ADJUSTWIDTH, 26 * ADJUSTHEIGHT, 90 * ADJUSTWIDTH, 35 * ADJUSTHEIGHT)];
    [backButton setImage:img1 forState:UIControlStateNormal];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backBack) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:backButton];
    [self.view addSubview:_titleView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    _webView.delegate =self;
    //设置webView不能上拉
    [(UIScrollView *)[[_webView subviews] objectAtIndex:0] setBounces:NO];
    if (self.model) {
        NSURLRequest * request1 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEWEB,self.model.cid]]];
        [_webView  loadRequest:request1];
        
    }
    else if (self.modelShopper)
    {
        NSURLRequest * request2 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEWEB,self.modelShopper.cid]]];
        [_webView  loadRequest:request2];
       
    }
    else if (self.modelNewCar)
    {
        NSURLRequest * request3 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEWEB,self.modelNewCar.cid]]];
        [_webView  loadRequest:request3];
     
    }
    else if (self.modelMark)
    {
        NSURLRequest * request4 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEWEB,self.modelMark.cid]]];
        [_webView  loadRequest:request4];
      
    }
    else if (self.modelMedia)
    {
        NSURLRequest * request5 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEWEB,self.modelMedia.cid]]];
        [_webView  loadRequest:request5];
        
    }
    else if (self.modelVideo)
    {
        NSURLRequest * request6 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEWEB,self.modelVideo.cid]]];
        [_webView  loadRequest:request6];
        
    }
    //活动
    else if (self.modelActivity)
    {
        NSURLRequest * request7 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEACTIVITY,self.strValueChange, self.modelActivity.cid]]];
        [_webView  loadRequest:request7];
    }
    //优惠
    else if (self.modelFavorable)
    {
        NSURLRequest * request8 = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:PRICEPREFERENTIAL,self.modelFavorable.dealerNewsId, self.modelFavorable.modelId]]];
        NSLog(@"%@",self.modelFavorable.modelId);
        [_webView  loadRequest:request8];
    }
    [self.view  addSubview:_webView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillExitFullscreen:) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillShowFullScreen:) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
}
- (UIImage *)scaleToSize : (UIImage *)img size:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
//加载完成的时候执行该方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   if ([LYYNightModeHandle shareNightModeHandle].temp == YES) {
    //字体颜色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    //页面背景色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
   }
    
}
//加载出错的时候执行该方法
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}
//进入全屏
-(void)playerWillShowFullScreen:(id)sender{
  
//     [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
//    _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//     _appDelegate.allowRotation = YES;
   
}
//开始加载的时候执行该方法
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//    //菊花
//    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    _HUD.removeFromSuperViewOnHide = YES;
//    _HUD.labelText = @"加载中....";
//    [_HUD hide:YES afterDelay:1];
//    [self.view addSubview:_HUD];
//    [_HUD setMode:MBProgressHUDModeIndeterminate];
//    self.view.backgroundColor = [UIColor colorWithHexCode:@"#ffe200"];
//    _handwritingLoader = [[FeHandwriting alloc] initWithView:self.view];
//    [self.view addSubview:_handwritingLoader];
//    
//    [_handwritingLoader showWhileExecutingBlock:^{
//        [self myTask];
//    } completion:^{
//        _handwritingLoader.hidden = YES;
//    }];
    _threeDot = [[FeThreeDotGlow alloc] initWithView:self.view blur:NO];
    [self.view addSubview:_threeDot];
    // Start
    [_threeDot showWhileExecutingBlock:^{
        [self myTask];
    } completion:^{
        _threeDot.hidden = YES;
    }];
    
    
}
- (void)myTask
{
    // Do something usefull in here instead of sleeping ...
    sleep(1.5);
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
-(void)playerWillExitFullscreen:(id)sender{

    
    
    /**
     下边方法的使用场景:
     如果点击视频,自动旋转为横屏播放状态,点击完成按钮,需要是程序变为竖屏状态,需要下边的代码
     */
//    UIViewController *vc = [[UIViewController alloc]init];
//    [self presentViewController:vc animated:NO completion:nil];
//    [vc dismissViewControllerAnimated:NO completion:nil];

    
//        _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//        _appDelegate.allowRotation = NO;
    
    //强制归正：
            UIDevice *device = [UIDevice currentDevice];
            [device setValue:[NSNumber numberWithInteger:UIInterfaceOrientationMaskPortrait] forKey:@"orientation"];
//    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
//        SEL selector = NSSelectorFromString(@"setOrientation:");
//        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
//        [invocation setSelector:selector];
//        [invocation setTarget:[UIDevice currentDevice]];
//        int val =UIInterfaceOrientationPortrait;
//        [invocation setArgument:&val atIndex:2];
//        [invocation invoke];
//    }
    
}

- (void)swi : (UISwipeGestureRecognizer *)swi

{
}
- (void)backBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
